package poo.GUI;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.io.*;

// This file gives an example of doing simple
// graphical user interface (GUI) in Java using 
// the Swing API.
//  
// You can use this file as a template for the GUI 
// part of your solution to Homework 4.
//
// The various classes involved are described
// briefly below.  The main thing you need to 
// modify (other than renaming classes) is the
// DemoPanel class, which is the GUI component
// that should display your tree.
public class PointDemo {
	public static Vector readListOfPoints(String inFile) throws IOException {
		FileReader fr = new FileReader(inFile);
		BufferedReader br = new BufferedReader(fr);

		StringTokenizer tokenizer;
		String line, token;
		int x, y;

		Vector vertices = new Vector();

		while ((line = br.readLine()) != null) {
			tokenizer = new StringTokenizer(line);

			if (tokenizer.countTokens() != 2)
				System.err.println("Wrong number of tokens in line");
			else {
				try {
					x = Integer.parseInt(tokenizer.nextToken());
					y = Integer.parseInt(tokenizer.nextToken());

					vertices.addElement(new Point(x, y));

				} catch (NumberFormatException e) {
					System.err.println("Wrong type of token " + e);
				}
			}
		}
		br.close();
		return vertices;
	}

	public static void main(String[] args) throws IOException {
		if (args.length < 1) {
			System.err.println("USAGE: java PointDemo <all points filename>");
			System.exit(1);
		}
		Vector allVertices;

		allVertices = readListOfPoints(args[0]);

		DemoFrame frame = new DemoFrame(allVertices);
		frame.setTitle("Point Demonstration");
		frame.setVisible(true);
	}

}

// Frame objects are movable frame windows; they
// can contain other GUI components like buttons,
// text areas, and panels. DemoFrame is a frame
// class that contains a panel; right now, the panel
// displays a hard-coded tree picture (see the
// class DemoPanel).

class DemoFrame extends JFrame {
	private DemoPanel panel;

	public DemoFrame(Vector allPts) {
		final int DEFAULT_FRAME_WIDTH = 500;
		final int DEFAULT_FRAME_HEIGHT = 500;

		// set the size of the frame window
		setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);

		// create and install a "listener object"
		// (event handler) to listen for window events
		WindowCloser listener = new WindowCloser();
		addWindowListener(listener);

		// create a panel object and install it in
		// the "content pane"
		panel = new DemoPanel(allPts);
		Container contentPane = getContentPane();
		contentPane.add(panel, "Center");

	}

	// The class WindowCloser is a listener (event
	// handler) class.
	private class WindowCloser extends WindowAdapter {
		// When the user closes the frame window, we
		// kill the program.
		public void windowClosing(WindowEvent event) {
			System.exit(0);
		}
	}
}

// THIS IS THE CLASS YOU NEED TO MODIFY.
// Panel objects are GUI components that you
// can draw on. A panel object should store
// as instance data all the information it needs
// to repaint itself.
class DemoPanel extends JPanel {
	private Vector allPoints;
	private static final int RADIUS = 2;// dimensione del punto.

	public DemoPanel(Vector allPts) {
		allPoints = allPts;
	}

	// The paintComponent method is called every time
	// that the panel needs to be displayed or refreshed.
	// Anything you want drawn on the panel should be drawn
	// in this method.

	public void paintComponent(Graphics page) {
		// leave the next line in
		super.paintComponent(page);
		setBackground(Color.black);

		// draw all points in red
		page.setColor(Color.green);

		for (int i = 0; i < allPoints.size(); i++) {
			Point currPoint = (Point) allPoints.elementAt(i);
			page.fillOval(currPoint.x - RADIUS, currPoint.y - RADIUS,
					2 * RADIUS, 2 * RADIUS);

			page.drawString("(" + currPoint.x + "," + currPoint.y + ")",
					currPoint.x + RADIUS, currPoint.y + RADIUS);
		}

	}
}
